package com.czl.module_base.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.CallSuper;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.module_base.R;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.databinding.FragmentBaseRecyclerViewBinding;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

/**
 * @author by xubin,
 * @date on 2022/1/26.
 * PS: Not easy to write code, please indicate.
 */
public abstract class BaseRecyclerViewActivity<V extends ViewBinding> extends BaseActivity<FragmentBaseRecyclerViewBinding> {
    protected boolean hasData;

    protected V mChildBinding;

    @Override
    protected FragmentBaseRecyclerViewBinding getBinding(LayoutInflater layoutInflater) {
        FragmentBaseRecyclerViewBinding viewBinding = FragmentBaseRecyclerViewBinding.inflate(layoutInflater);
        mChildBinding = getChildBinding(layoutInflater, viewBinding.containerTop);
        return viewBinding;
    }

    /**
     * 子布局
     *
     * @param layoutInflater
     * @param containerTop
     * @return
     */
    protected abstract V getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop);

    @CallSuper
    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        binding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.setEnableLoadMore(true);
            loadData();
        });
        binding.smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> loadMore());
    }

    protected void loadMore() {

    }

    @Override
    public void showLoading() {
        if (!hasData) {
            super.showLoading();
        }
    }

    @Override
    protected View getLoadView() {
        return binding.smartRefreshLayout;
    }

    protected <T> void handleResponseData(BaseQuickAdapter<T, ? extends BaseViewHolder> adapter,
                                          int page,
                                          ListBean<T> list) {

        if (binding.recyclerView.getAdapter() == null) {
            binding.recyclerView.setAdapter(adapter);
        }
        SmartRefreshLayout refreshLayout = binding.smartRefreshLayout;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        showLoadSuccess();
        if (page == 1) {
            if (list == null || list.getList() == null || list.getList().isEmpty()) {
                hasData = false;
                adapter.setList(new ArrayList<>());
                adapter.removeEmptyView();
                View emptyView = View.inflate(getContext(), R.layout.common_empty_layout, null);
                emptyView.findViewById(R.id.ll_empty).setOnClickListener(v -> refreshLayout.autoRefresh());
                adapter.setEmptyView(emptyView);
                refreshLayout.setEnableLoadMore(false);
                return;
            }
            hasData = true;
            adapter.setList(list.getList());
        } else {
            hasData = true;
            if (list != null && list.getList() != null) {
                adapter.addData(list.getList());
            }
        }
        if (list == null || list.getTotal() == adapter.getData().size() || !list.hasNextPage()) {
            refreshLayout.finishLoadMoreWithNoMoreData();
        } else {
            refreshLayout.setEnableLoadMore(true);
            refreshLayout.resetNoMoreData();
        }
    }

    @Override
    public void showEmpty() {
        super.showEmpty();
        hasData = false;
        RecyclerView.Adapter<?> adapter = binding.recyclerView.getAdapter();
        if (adapter instanceof BaseQuickAdapter<?, ?>) {
            BaseQuickAdapter<?, ?> baseQuickAdapter = (BaseQuickAdapter<?, ?>) adapter;
            baseQuickAdapter.setList(new ArrayList<>());
            baseQuickAdapter.removeEmptyView();
            View emptyView = View.inflate(getContext(), R.layout.common_empty_layout, null);
            emptyView.findViewById(R.id.ll_empty).setOnClickListener(v -> binding.smartRefreshLayout.autoRefresh());
            baseQuickAdapter.setEmptyView(emptyView);
            binding.smartRefreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mChildBinding != null && mChildBinding instanceof ViewDataBinding) {
            ((ViewDataBinding) mChildBinding).unbind();
        }
    }
}
